import nodemailer from 'nodemailer';
import config from '../config';

const transporter = nodemailer.createTransport({
    host: config.MAIL.HOST,
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
        user: config.MAIL.USER, // generated ethereal user
        pass: config.MAIL.PASS, // generated ethereal password
    },
});

export default async ({ email, activationCode }: { email: string; activationCode: string }) => {
    const html = `
    <div style="font-size:15px;padding:30px;background-color: #f6f6f6;color: #31302f;">
                        <div style="background-color: #fff;width:300px;margin: 0 auto;position: relative;padding: 20px 30px 20px 30px;text-align: center;">
                            <img src="#" style="height: 32px;" />
                            <p>亲爱的用户，您好！</p>
                            <p>您正在进行 Personal Calendar 账号操作</p>
                            <p>请点击下方链接进行激活</p>
                            <a style="color:#569ccb;font-size:30px" href="http://127.0.0.1:12306/activate?email=${email}&activationCode=${activationCode}">点我激活账号</a>
                            <p>请确认你在进行激活操作，不要向其他人泄露该链接</p>
                            <p>该激活链接5分钟内有效</p>
                            <p style="font-size: 13px;color: #6b7280;margin-top: 30px">©️ Copyright 起飞 | 2022</p>
                        </div>
                    </div>
    ${activationCode}
    `;

    return new Promise((resolve, reject) => {
        transporter
            .sendMail({
                from: `"personal-calendar 👻 "<${config.MAIL.USER}>`,
                to: email,
                subject: '激活personal-calendar账号 ✔',
                text: '激活personal-calendar账号 ✔',
                html,
            })
            .then(info => {
                console.log('Message sent: %s', info.messageId);
                resolve(info);
            })
            .catch(err => {
                reject(err);
            });
    });
};
